package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author morucheng
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2024-10-30 15:12:52
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;


    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueServiceImpl apartmentFeeValueService;

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    DistrictInfoService districtInfoService;


    @Override
    public IPage<ApartmentItemVo> pageApart(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApart(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);


        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoByTypeAndId(ItemType.APARTMENT, id);


        List<LabelInfo> labelInfoList = labelInfoMapper.selectByTypeAndId(ItemType.APARTMENT, id);


        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByTypeAndId(ItemType.APARTMENT, id);


        List<FeeValueVo> feeValueVoList = feeValueMapper.selectByAparId(id);

        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);


        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApart(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();
        if (isUpdate) {
            //修改
            //先删除

            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentId);

            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        }



        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos = graphVoList.stream().map(
                    graphVo -> {
                        GraphInfo graphInfo = new GraphInfo();
                        graphInfo.setName(graphVo.getName());
                        graphInfo.setUrl(graphVo.getUrl());
                        graphInfo.setItemType(ItemType.APARTMENT);
                        graphInfo.setItemId(apartmentId);
                        return graphInfo;
                    }).toList();
            graphInfoService.saveBatch(graphInfos);
        }


        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<ApartmentFacility> apartmentFacilityList = facilityInfoIds.stream().map(
                    facilityInfoId -> ApartmentFacility.builder()
                            .apartmentId(apartmentId)
                            .facilityId(facilityInfoId)
                            .build()
            ).toList();
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }


        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = labelIds.stream().map(
                    labelId -> ApartmentLabel.builder()
                            .apartmentId(apartmentId)
                            .labelId(labelId)
                            .build()
            ).toList();
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentFeeValueList = feeValueIds.stream().map(
                    feeValueId -> ApartmentFeeValue.builder()
                            .apartmentId(apartmentId)
                            .feeValueId(feeValueId)
                            .build()
            ).toList();
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        String provinceName = provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName();
        this.update(new LambdaUpdateWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,apartmentId).set(ApartmentInfo::getProvinceName,provinceName));

        String cityName = cityInfoService.getById(apartmentSubmitVo.getCityId()).getName();
        this.update(new LambdaUpdateWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,apartmentId).set(ApartmentInfo::getCityName,cityName));

        String districtName = districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName();
        this.update(new LambdaUpdateWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,apartmentId).set(ApartmentInfo::getDistrictName,districtName));

    }

    @Override
    public void removeApartById(Long id) {

        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        if(roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        super.removeById(id);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);

        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }

}




